package com.fansl.allround.auth.provider.social.qq.api;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fansl.allround.auth.provider.social.qq.bean.QQOpenId;
import com.fansl.allround.auth.provider.social.qq.bean.QQToken;
import com.fansl.allround.auth.provider.social.qq.bean.QQUserInfo;
import com.fansl.allround.auth.provider.social.qq.config.QQOauthConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author fansl
 * @Description: 不同用户信息不一样 不能直接@Component(spring默认单例 线程不安全)
 * @date 2019/10/15 18:14
 */
@Slf4j
@Service
public class QQApiImpl implements QQApi {
    /**
     * 将用户导向认证服务器
     */
    public static final String URL_AUTHORIZE = "https://graph.qq.com/oauth2.0/authorize";
    /**
     * 随带授权码获取用户令牌accessToken
     */
    public static final String URL_ACCESS_TOKEN = "https://graph.qq.com/oauth2.0/token" +
            "?grant_type=%s&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s";
    /**
     * 获取openID   http://wiki.connect.qq.com/%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7openid_oauth2-0
     */
    public final static String URL_GET_OPENID = "https://graph.qq.com/oauth2.0/me?access_token=%s";
    /**
     * 获取用户信息 父类会自动携带accessToken http://wiki.connect.qq.com/get_user_info
     */
    public final static String URL_GET_USER_INFO = "https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s";

    @Resource
    private QQOauthConfig qqOauthConfig;

    /**
     * 获取用户信息
     *
     * @return
     */
    @Override
    public QQUserInfo getUserInfo(String accessToken, String openId) {
        String url = String.format(URL_GET_USER_INFO, accessToken, qqOauthConfig.getAppId(), openId);
        String userInfoResult = HttpUtil.createGet(url).execute().body();
        log.info("qq getUserInfo json response:{}", userInfoResult);
        QQUserInfo userInfo = JSONObject.parseObject(userInfoResult, new TypeReference<QQUserInfo>() {
        });
        return userInfo;
    }

    /**
     * 获取accessToken
     *
     * @return
     */
    @Override
    public QQToken getAccessToken(String code) {
        String grantType = "authorization_code";
        String redirectUri = URLUtil.encode(qqOauthConfig.getRedirectUri(), "utf-8");
        String url = String.format(URL_ACCESS_TOKEN, grantType,
                qqOauthConfig.getAppId(), qqOauthConfig.getAppSecret(), code, redirectUri);
        String result = HttpUtil.createGet(url).execute().body();
        log.info("qq getAccessToken json response:{}", result);
        return null;
    }

    /**
     * 根据accessToken获取openId
     *
     * @param accessToken
     * @return
     */
    @Override
    public QQOpenId getOpenId(String accessToken) {
        String url = String.format(URL_GET_OPENID, accessToken);
        String result = HttpUtil.createGet(url).execute().body();
        log.info("qq getOpenId json response:{}", result);
        return null;
    }
}
